package com.sjgtw.cloud.system.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.common.security.annotation.PreAuthorize;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.system.domain.SysUserOnline;
import com.sjgtw.cloud.system.service.ISysUserOnlineService;

/**
 * <p>Title: 在线用户会话管理</p>
 * <p>Description: </p>
 * @author Admin
 * @date 2020-7-27
 */
@RestController
@RequestMapping("/online")
public class SysUserOnlineController extends BaseController
{
	@Autowired
	private ISysUserOnlineService userOnlineService;

	@Autowired
	private RedisService redisService;

	@PreAuthorize(hasPermi = "monitor:online:list")
	@GetMapping("/list")
	public TableDataInfo list(String ipaddr, String userName) {
		Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
		List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
		for (String key : keys) {
			LoginUser user = redisService.getCacheObject(key);
			if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
				if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
					userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
				}
			} else if (StringUtils.isNotEmpty(ipaddr)) {
				if (StringUtils.equals(ipaddr, user.getIpaddr())) {
					userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
				}
			} else if (StringUtils.isNotEmpty(userName)) {
				if (StringUtils.equals(userName, user.getUsername())) {
					userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
				}
			} else {
				userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
			}
		}
		Collections.reverse(userOnlineList);
		userOnlineList.removeAll(Collections.singleton(null));
		return getDataTable(userOnlineList);
	}

	 /**
     * <p>Title: 强制退出在线用户</p>
     * <p>Description: </p>
     * @param tokenId
     * @return
     */
	@PreAuthorize(hasPermi = "monitor:online:forceLogout")
	@Log(title = "在线用户", businessType = BusinessType.FORCE)
	@DeleteMapping("/{tokenId}")
	public AjaxResult forceLogout(@PathVariable String tokenId) {
		redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
		return AjaxResult.success();
	}
}
